package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.XYAnchor;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.Request;

import edu.pku.sei.modeler.figures.TimingLifeLineFigure;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.BasicInteractions.StateInvariant;
import edu.pku.sei.modeler.model.uml.temp.timeline.TimingLifeLine;

public class TimingLifeLineEditpart extends AbstractUMLNodeEditPart {
	public static final int MARGIN = 5;
	public TimingLifeLineEditpart(MetaShapeElement model) {
		super(model);
	}
	
	@Override
	protected IFigure createFigure() {
		// TODO Auto-generated method stub
		TimingLifeLineFigure figure = new TimingLifeLineFigure();
		TimingLifeLine model = (TimingLifeLine) getNode().getModelElement();
		ArrayList<String> names = new ArrayList<String>();
		for (int i = 0; i < model.getIncludedStateInvariants().size(); i++) {
			names.add(((StateInvariant)model.getIncludedStateInvariants().get(i)).getName());
		}
		figure.setStateName(names);
		return figure;
	}
	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();
		TimingLifeLine model = (TimingLifeLine) getNode().getModelElement();
		ArrayList<String> names = new ArrayList<String>();
		for (int i = 0; i < model.getIncludedStateInvariants().size(); i++) {
			names.add(((StateInvariant)model.getIncludedStateInvariants().get(i)).getName());
		}
		((TimingLifeLineFigure)figure).setStateName(names);
		
		
	}
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		super.handlePropertyChanged(event);
		refreshVisuals();
	}
	
	@Override
	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		Rectangle timeLineAreaBounds = ((TimingLifeLineFigure)getFigure()).getTimeLineArea().getBounds();
		return new XYAnchor(new Point(timeLineAreaBounds.x + MARGIN, timeLineAreaBounds.y + timeLineAreaBounds.height - MARGIN));
	}
	
	@Override
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		Rectangle timeLineAreaBounds = ((TimingLifeLineFigure)getFigure()).getTimeLineArea().getBounds();
		return new XYAnchor(new Point(timeLineAreaBounds.x + MARGIN, timeLineAreaBounds.y + timeLineAreaBounds.height - MARGIN));
	}
	
	@Override
	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		Rectangle timeLineAreaBounds = ((TimingLifeLineFigure)getFigure()).getTimeLineArea().getBounds();
		return new XYAnchor(new Point(timeLineAreaBounds.x +timeLineAreaBounds.width - MARGIN, timeLineAreaBounds.y+ timeLineAreaBounds.height -MARGIN));
	}
	
	@Override
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		Rectangle timeLineAreaBounds = ((TimingLifeLineFigure)getFigure()).getTimeLineArea().getBounds();
		return new XYAnchor(new Point(timeLineAreaBounds.x +timeLineAreaBounds.width - MARGIN, timeLineAreaBounds.y+ timeLineAreaBounds.height -MARGIN));
	}
}
